#ifndef __EJ_EVENT__
#define __EJ_EVENT__

#include "common_def.h"

/* 事件处理函数类型 */
typedef void (*event_handler_t)(uint32_t id, void *message, uint32_t msg_size);

/* 任务上下文结构 */
typedef struct {
    event_handler_t handler;
    volatile uint32_t pending_count;
} TaskContext_t;

/* 平台适配接口 */
typedef struct {
    /* 临界区保护 */
    void (*enter_critical)(void);
    void (*exit_critical)(void);

    /* 时间基准（毫秒单位） */
    uint64_t (*get_system_time)(void);

    /* 内存管理 */
    void* (*mem_alloc)(size_t size);
    void (*mem_free)(void* ptr);
} EventPlatform_t;

#define EJ_MS(ms)    ((uint32_t) (ms))

#define EJ_SEC(s)    ((uint32_t) ((s) * (uint32_t) 1000))

#define EJ_MIN(m)    ((uint32_t) ((m) * (uint32_t) 1000 * (uint32_t) 60))

#define EJ_HOUR(h)   ((uint32_t) ((h) * (uint32_t) 1000 * (uint32_t) 60) * (uint32_t) 60)

/* 核心API */
void ej_event_init(const EventPlatform_t* platform);
void ej_event_create_task(TaskContext_t* ctx, event_handler_t handler);
void ej_event_publish_datas(TaskContext_t* ctx, uint32_t event_id, void* msg, size_t msg_size, uint32_t uint32_t_ms);
void ej_event_publish(TaskContext_t* ctx, uint32_t event_id, uint32_t uint32_t_ms);

void ej_event_cancel(TaskContext_t* ctx, uint32_t event_id);
void ej_event_process(void);


/* 中断上下文API */
void ej_event_cancel_isr(TaskContext_t* ctx, uint32_t event_id);
void ej_event_publish_datas_isr(TaskContext_t* ctx, uint32_t event_id, void* msg, size_t msg_size, uint32_t uint32_t_ms);
void ej_event_publish_isr(TaskContext_t* ctx, uint32_t event_id, uint32_t uint32_t_ms);

#endif  /*__EJ_EVENT__*/
